package com.doubao.file.controller;

import com.doubao.common.result.Result;
import com.doubao.file.dto.FileInfoResponse;
import com.doubao.file.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件控制器
 */
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "文件管理", description = "文件上传下载相关接口")
public class FileController {

    private final FileService fileService;

    /**
     * 上传用户头像
     */
    @PostMapping("/avatar")
    @Operation(summary = "上传用户头像", description = "上传用户头像图片")
    public Result<FileInfoResponse> uploadAvatar(
            @RequestParam("file") MultipartFile file,
            @RequestParam("userId") Long userId) {
        try {
            FileInfoResponse fileInfo = fileService.uploadAvatar(file, userId);
            return Result.success(fileInfo);
        } catch (Exception e) {
            log.error("上传头像失败", e);
            return Result.failed("上传头像失败: " + e.getMessage());
        }
    }

    /**
     * 上传聊天记录文件
     */
    @PostMapping("/chat/record")
    @Operation(summary = "上传聊天记录文件", description = "上传聊天记录相关文件")
    public Result<FileInfoResponse> uploadChatRecord(
            @RequestParam("file") MultipartFile file,
            @RequestParam("userId") Long userId,
            @RequestParam("chatId") String chatId) {
        try {
            FileInfoResponse fileInfo = fileService.uploadChatRecord(file, userId, chatId);
            return Result.success(fileInfo);
        } catch (Exception e) {
            log.error("上传聊天记录文件失败", e);
            return Result.failed("上传聊天记录文件失败: " + e.getMessage());
        }
    }

    /**
     * 保存聊天消息为文件
     */
    @PostMapping("/chat/message")
    @Operation(summary = "保存聊天消息", description = "将聊天消息保存为文本文件")
    public Result<FileInfoResponse> saveChatMessage(
            @RequestParam("userId") Long userId,
            @RequestParam("chatId") String chatId,
            @RequestParam("content") String content,
            @RequestParam("messageId") String messageId) {
        try {
            FileInfoResponse fileInfo = fileService.saveChatMessage(userId, chatId, content, messageId);
            return Result.success(fileInfo);
        } catch (Exception e) {
            log.error("保存聊天消息失败", e);
            return Result.failed("保存聊天消息失败: " + e.getMessage());
        }
    }

    /**
     * 获取头像URL
     */
    @GetMapping("/avatar/url")
    @Operation(summary = "获取头像URL", description = "获取用户头像的临时访问URL")
    public Result<String> getAvatarUrl(@RequestParam("objectName") String objectName) {
        try {
            String url = fileService.getAvatarUrl(objectName);
            return Result.success(url);
        } catch (Exception e) {
            log.error("获取头像URL失败", e);
            return Result.failed("获取头像URL失败: " + e.getMessage());
        }
    }

    /**
     * 获取聊天记录URL
     */
    @GetMapping("/chat/url")
    @Operation(summary = "获取聊天记录URL", description = "获取聊天记录文件的临时访问URL")
    public Result<String> getChatRecordUrl(@RequestParam("objectName") String objectName) {
        try {
            String url = fileService.getChatRecordUrl(objectName);
            return Result.success(url);
        } catch (Exception e) {
            log.error("获取聊天记录URL失败", e);
            return Result.failed("获取聊天记录URL失败: " + e.getMessage());
        }
    }

    /**
     * 删除文件
     */
    @DeleteMapping("/{objectName}")
    @Operation(summary = "删除文件", description = "删除指定的文件")
    public Result<Void> deleteFile(@PathVariable("objectName") String objectName) {
        try {
            fileService.deleteFile(objectName);
            return Result.success();
        } catch (Exception e) {
            log.error("删除文件失败", e);
            return Result.failed("删除文件失败: " + e.getMessage());
        }
    }

    /**
     * 获取聊天记录文件列表
     */
    @GetMapping("/chat/{chatId}")
    @Operation(summary = "获取聊天记录文件列表", description = "获取指定聊天的文件列表")
    public Result<List<FileInfoResponse>> getChatFiles(
            @PathVariable("chatId") String chatId,
            @RequestParam("userId") Long userId) {
        try {
            List<FileInfoResponse> files = fileService.getChatFiles(userId, chatId);
            return Result.success(files);
        } catch (Exception e) {
            log.error("获取聊天文件列表失败", e);
            return Result.failed("获取聊天文件列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取用户头像文件列表
     */
    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户头像文件列表", description = "获取指定用户的头像文件列表")
    public Result<List<FileInfoResponse>> getUserFiles(@PathVariable("userId") Long userId) {
        try {
            List<FileInfoResponse> files = fileService.getUserFiles(userId);
            return Result.success(files);
        } catch (Exception e) {
            log.error("获取用户头像文件列表失败", e);
            return Result.failed("获取用户头像文件列表失败: " + e.getMessage());
        }
    }
}